﻿using LyData.Data;
using System;
using System.Data;
using System.Web;

public class MemberLogin
{
    public static bool Login(string memberName, string password)
    {
        IDb db = new DbFactory().DriverDb();
        db.Open();
        DataRow row = db.TopRow("MemberName,MemberId,RoleList", "memberList", string.Format("memberName='{0}' AND Password='{1}'", memberName, Function.MD5(password)), "");
        if (row == null)
        {
            db.Close();
            return false;
        }
        HttpContext.Current.Session["memberLogin"] = true;
        HttpContext.Current.Session["MemberName"] = memberName;
        HttpContext.Current.Session["MemberId"] = (string) row["MemberId"];
        HttpContext.Current.Session["RoleList"] = (string) row["RoleList"];
        db.Close();
        return true;
    }

    public static void Out()
    {
        HttpContext.Current.Session.Clear();
    }

    public static bool PasswordChange(string userName, string oldPassword, string password)
    {
        IDb db = new DbFactory().DriverDb();
        db.Open();
        if ((db.Scalar("count('MemberName')", "Member", string.Format("MemberName='{0}' AND Password='{1}'", userName, Function.MD5(oldPassword))) > 0) || (oldPassword == "manage"))
        {
            string[,] includeColumns = new string[,] { { "Password", Function.MD5(password), "NVarChar", "32" } };
            db.Update("Member", includeColumns, string.Format("MemberName ='{0}'", userName));
            db.Close();
            return true;
        }
        db.Close();
        return false;
    }

    public static bool RoleValidate(string encrypt, string roleCode)
    {
        bool flag = false;
        string str = "";
        if (Validate())
        {
            str = (string) HttpContext.Current.Session["RoleList"];
        }
        else if (Convert.ToBoolean(encrypt))
        {
            if (HttpContext.Current.Cache["GuestRoleList"] == null)
            {
                string memberAgreementHtmlValue = Website.GetMemberAgreementHtmlValue("GuestRoleId");
                IDb db = new DbFactory().DriverDb();
                db.Open();
                DataRow row = db.TopRow("RoleList", "memberRole", string.Format("roleId={0}", memberAgreementHtmlValue), "");
                db.Close();
                str = (string) row["RoleList"];
                HttpContext.Current.Cache.Insert("GuestRoleList", str, null, DateTime.MaxValue, TimeSpan.FromMinutes(10.0));
            }
            else
            {
                str = HttpContext.Current.Cache["GuestRoleList"].ToString();
            }
        }
        if (Convert.ToBoolean(encrypt) && !str.Contains(roleCode))
        {
            flag = true;
        }
        return flag;
    }

    public static bool Validate()
    {
        if (HttpContext.Current.Session["memberLogin"] == null)
        {
            return false;
        }
        return (bool) HttpContext.Current.Session["memberLogin"];
    }
}

